﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Practices.Prism.Mvvm;
using VisaCRM.Modules.Admin.Model;
using System.Collections.ObjectModel;
using System.Windows.Input;
using VisaCRM.Modules.Admin.Gateway;
using VisaCRM.Modules.Admin.ViewModels.Helper;


namespace VisaCRM.Modules.Admin.ViewModels
{
    /// <summary>
    /// ViewModel of an list of Consultants
    /// </summary>
    class ConsultantViewModel:BindableBase
    {
        /// <summary>
        /// initialises all collection used
        /// </summary>
        private ObservableCollection<Consultant> allConsultant { get; set; }
        public ObservableCollection<Department> AvailableDepartment { get; set; }
        public ObservableCollection<Consultant> AvailableSupervisor { get; set; }
        public ObservableCollection<Branch> AvailableBranch { get; set; }
        public ObservableCollection<AccessLevel> AvailableAccessLevel { get; set; }
        public ObservableCollection<string> AvailableStatus { get; set; }
        
        /// <summary>
        /// initialise gateway used
        /// </summary>
        ConsultantGateway consultantGateway = new ConsultantGateway();
        DepartmentGateway departmentGateway = new DepartmentGateway();
        BranchGateway branchGateway = new BranchGateway();
        AccessLevelGateway accessLevelGateway = new AccessLevelGateway();

        ///summary
        ///initialises a new instance of ConsultantViewModel class
       ///summary
        public ConsultantViewModel(ObservableCollection<Consultant> allconsultant)
       {
           currentConsultant = new Consultant();
           if (allconsultant == null || allconsultant.Count == 0)
           {

               AllConsultant = consultantGateway.GetAllConsultants();
           }
           else
           {
               this.AllConsultant = allconsultant;
           }

           /// Re-act to any changes from outside this ViewModel
           this.AllConsultant.CollectionChanged += (sender, e) =>
           {
               if (e.OldItems != null && e.OldItems.Contains(this.CurrentConsultant))
               {
                   this.CurrentConsultant = null;
               }
           };

           this.AvailableDepartment = departmentGateway.GetAlldeparments();
           this.AvailableSupervisor = consultantGateway.GetAllSupervisor();
           this.AvailableAccessLevel = accessLevelGateway.GetAllAccessLevel();
           this.AvailableBranch = branchGateway.GetAllBranch();
            this.AvailableStatus= consultantGateway.GetAllStatus();

           this.SaveCommand = new DelegateCommand((o) => this.Save());
           this.AddNewCommand = new DelegateCommand((o) => this.AddNew());
           this.CancelCommand = new DelegateCommand((o) => this.Cancel());
           this.DeleteCommand = new DelegateCommand((o) => this.Delete());
           this.EditCommand = new DelegateCommand((o) => this.Edit());
       }

        public ICommand AddNewCommand { get; private set; }
        public ICommand SaveCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }
        public ICommand EditCommand { get; private set; }

        private void Save()
        {
            if (this.CurrentConsultant != null)
            {
                Consultant consultant = new Consultant()
                {
                    ConsultantName = this.CurrentConsultantName,
                    ConsultantBranchID = this.CurrentConsultantBranch.BranchID,
                    ConsultantAccreditation = this.CurrentConsultantAccreditationDetail,
                    ConsultantDepartment = this.CurrentConsultantDepartment.DepartmentID,
                    ConsultantSupervisorID = this.CurrentConsultantSupervisor.ConsultantID,
                    ConsultantEmail = this.CurrentConsultantEmail,
                    ConsultantDDI = this.CurrentConsultantDDI,
                    ConsultantDDIUK = this.CurrentConsultantTeamDDI,
                    ConsultantJobTitle = this.CurrentConsultantJobTitle,
                    ConsultantSecurityLevel = this.CurrentConsultantAccessLevel.SecurityID,

                };
                if (this.CurrentConsultantStatus == "Active")
                    consultant.ConsultantStatus = 1;
                else
                    consultant.ConsultantStatus = 0;
                bool flag = false;
                if (string.IsNullOrEmpty(consultant.ConsultantName))
                    return;
                foreach (Consultant c in this.AllConsultant)
                {
                    if (c.ConsultantName == consultant.ConsultantName)
                    {
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    System.Windows.Forms.MessageBox.Show("The consultant details are already listed");
                    return;
                }
                consultantGateway.SaveNewConsultantToDatabase(consultant.ConsultantName, consultant.ConsultantBranchID, consultant.ConsultantAccreditation, consultant.ConsultantDepartment, consultant.ConsultantSupervisorID, consultant.ConsultantEmail, consultant.ConsultantDDI, consultant.ConsultantDDIUK, consultant.ConsultantJobTitle, consultant.ConsultantSecurityLevel, consultant.ConsultantStatus);
                AddNew();
                this.AllConsultant = consultantGateway.GetAllConsultants();
                System.Windows.Forms.MessageBox.Show("The Consultant details are saved sucessfully");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Please fill Consultant details");
            }
        }

        private void AddNew()
        {
            this.CurrentConsultantAccreditationDetail = null;
            this.CurrentConsultantDDI = null;
            this.CurrentConsultantEmail = null;
            this.CurrentConsultantBranch = null;
            this.CurrentConsultantDepartment = null;
            this.CurrentConsultantJobTitle = null;
            this.CurrentConsultantStatus = null;
            this.CurrentConsultantName = null;
            this.CurrentConsultantSupervisor = null;
            this.CurrentConsultantTeamDDI = null;
            this.CurrentConsultantAccessLevel = null;
        }
        private void Cancel()
        {
            this.CurrentConsultant = null;
        }
        private void Delete()
        {
            if (this.CurrentConsultant != null)
            {
                consultantGateway.DeleteConsultantFromDatabase(this.CurrentConsultant.ConsultantID);
                AllConsultant.Remove(this.CurrentConsultant);
                AddNew();
                System.Windows.Forms.MessageBox.Show("The Consultant details is deleted sucessfully");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Please select Consultant details to be deleted");
            }

        }

        private void Edit()
        {
            if (this.CurrentConsultant != null)
            {
                Consultant consultant = new Consultant()
                {
                    ConsultantName = this.CurrentConsultantName,
                    ConsultantBranchID = this.CurrentConsultantBranch.BranchID,
                    ConsultantAccreditation = this.CurrentConsultantAccreditationDetail,
                    ConsultantDepartment = this.CurrentConsultantDepartment.DepartmentID,
                    ConsultantSupervisorID = this.CurrentConsultantSupervisor.ConsultantID,
                    ConsultantEmail = this.CurrentConsultantEmail,
                    ConsultantDDI = this.CurrentConsultantDDI,
                    ConsultantDDIUK= this.CurrentConsultantTeamDDI,
                    ConsultantJobTitle = this.CurrentConsultantJobTitle,
                    ConsultantID = this.CurrentConsultant.ConsultantID,
                    ConsultantSecurityLevel=this.CurrentConsultantAccessLevel.SecurityID,
                };

                if (this.CurrentConsultantStatus == "Active")
                    consultant.ConsultantStatus = 1;
                else
                    consultant.ConsultantStatus = 0;

                this.CurrentConsultant = consultant;
                consultantGateway.EditConsultantToDatabase(consultant.ConsultantID, consultant.ConsultantName, consultant.ConsultantBranchID, consultant.ConsultantAccreditation, consultant.ConsultantDepartment, consultant.ConsultantSupervisorID, consultant.ConsultantEmail, consultant.ConsultantDDI, consultant.ConsultantDDIUK,consultant.ConsultantJobTitle,consultant.ConsultantSecurityLevel,consultant.ConsultantStatus);
                this.AllConsultant = consultantGateway.GetAllConsultants();
                this.CurrentConsultant = consultant;
                System.Windows.Forms.MessageBox.Show("The Consultant details is editted sucessfully");
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Please select Consultant details to be edited");
            }
        }

        /// <summary>
        /// The Consultant currently selected in the workspace
        /// </summary>
        private Consultant currentConsultant;

        /// <summary>
        /// Gets or sets the Consultants currently selected in the workspace
        /// </summary>
        public Consultant CurrentConsultant
        {
            get
            {
                return this.currentConsultant;
            }

            set
            {
                this.currentConsultant = value;
                if (this.CurrentConsultant != null)
                {
                    this.CurrentConsultantName = this.CurrentConsultant.ConsultantName;
                    this.CurrentConsultantJobTitle = this.CurrentConsultant.ConsultantJobTitle;
                    this.CurrentConsultantDDI = this.CurrentConsultant.ConsultantDDI;
                    this.CurrentConsultantTeamDDI = this.CurrentConsultant.ConsultantDDIUK;
                    this.CurrentConsultantEmail = this.CurrentConsultant.ConsultantEmail;
                    this.CurrentConsultantAccreditationDetail = this.CurrentConsultant.ConsultantAccreditation;
                    this.CurrentConsultantStatus = AvailableStatus.FirstOrDefault(s => s == (consultantGateway.GetCurrentConsultantStatus(this.CurrentConsultant.ConsultantStatus)));
                    this.CurrentConsultantDepartment = AvailableDepartment.FirstOrDefault(dep => dep.DepartmentName == (departmentGateway.GetCurrentDepartment(this.currentConsultant.ConsultantDepartment)).DepartmentName);
                    this.CurrentConsultantSupervisor = AvailableSupervisor.FirstOrDefault(sv => sv.ConsultantName == (consultantGateway.GetCurrentSupervisor(this.currentConsultant.ConsultantSupervisorID)).ConsultantName);
                    this.CurrentConsultantBranch = AvailableBranch.FirstOrDefault(b => b.BranchName == (branchGateway.GetCurrentbranch(this.currentConsultant.ConsultantBranchID)).BranchName);
                    this.CurrentConsultantAccessLevel= AvailableAccessLevel.FirstOrDefault(al=> al.SecurityName==(accessLevelGateway.GetCurrentAccessLevel(this.CurrentConsultant.ConsultantSecurityLevel)).SecurityName);
                }
                this.OnPropertyChanged("CurrentConsultant");
            }
        }

        /// <summary>
        /// The Supervisor currently selected in the workspace
        /// </summary>
        private Consultant currentConsultantSupervisor;

        /// <summary>
        /// Gets or sets the Consultants currently selected in the workspace
        /// </summary>
        public Consultant CurrentConsultantSupervisor
        {
            get
            {
                return this.currentConsultantSupervisor;
            }
            set
            {
                this.currentConsultantSupervisor = value;
                this.OnPropertyChanged("CurrentConsultantSupervisor");
            }
        }

        /// <summary>
        /// The ConsultantAccessLevel currently selected in the workspace
        /// </summary>
        private AccessLevel currentConsultantAccessLevel;

        /// <summary>
        /// Gets or sets the ConsultantAccessLevel currently selected in the workspace
        /// </summary>
        public AccessLevel CurrentConsultantAccessLevel
        {
            get
            {
                return this.currentConsultantAccessLevel;
            }
            set
            {
                this.currentConsultantAccessLevel = value;
                this.OnPropertyChanged("CurrentConsultantAccessLevel");
            }
        }

        /// <summary>
        /// Gets or sets the ConsultantList currently selected in the workspace
        /// </summary>
        public ObservableCollection<Consultant> AllConsultant
        {
            get
            {
                return this.allConsultant;
            }

            set
            {
                this.allConsultant = value;
                this.OnPropertyChanged("AllConsultant");
            }
        }

        /// <summary>
        /// the ConsultantName currently selected in the workspace
        /// </summary>
        private string currentConsultantName;
        /// <summary>
        /// Gets or sets the ConsultantName currently selected in the workspace
        /// </summary>
        public string CurrentConsultantName
        {
            get
            {
                return this.currentConsultantName;
            }

            set
            {
                this.currentConsultantName = value;
                this.OnPropertyChanged("CurrentConsultantName");
            }
        }

        /// <summary>
        /// the ConsultantJobTitle currently selected in the workspace
        /// </summary>
        private string currentConsultantJobTitle;
        /// <summary>
        /// Gets or sets the ConsultantJobTitle currently selected in the workspace
        /// </summary>
        public string CurrentConsultantJobTitle
        {
            get
            {
                return this.currentConsultantJobTitle;
            }

            set
            {
                this.currentConsultantJobTitle = value;
                this.OnPropertyChanged("CurrentConsultantJobTitle");
            }
        }

        /// <summary>
        /// the ConsultantDDI currently selected in the workspace
        /// </summary>
        private string currentConsultantDDI;
        /// <summary>
        /// Gets or sets the ConsultantDDI currently selected in the workspace
        /// </summary>
        public string CurrentConsultantDDI
        {
            get
            {
                return this.currentConsultantDDI;
            }

            set
            {
                this.currentConsultantDDI = value;
                this.OnPropertyChanged("CurrentConsultantDDI");
            }
        }

        /// <summary>
        /// the ConsultantTeamDDI currently selected in the workspace
        /// </summary>
        private string currentConsultantTeamDDI;
        /// <summary>
        /// Gets or sets the ConsultantTeamDDI currently selected in the workspace
        /// </summary>
        public string CurrentConsultantTeamDDI
        {
            get
            {
                return this.currentConsultantTeamDDI;
            }

            set
            {
                this.currentConsultantTeamDDI = value;
                this.OnPropertyChanged("CurrentConsultantTeamDDI");
            }
        }

        /// <summary>
        /// the ConsultantEmail currently selected in the workspace
        /// </summary>
        private string currentConsultantEmail;
        /// <summary>
        /// Gets or sets the ConsultantEmail currently selected in the workspace
        /// </summary>
        public string CurrentConsultantEmail
        {
            get
            {
                return this.currentConsultantEmail;
            }

            set
            {
                this.currentConsultantEmail = value;
                this.OnPropertyChanged("CurrentConsultantEmail");
            }
        }


        /// <summary>
        /// the ConsultantStatus currently selected in the workspace
        /// </summary>
        private string currentConsultantStatus;
        /// <summary>
        /// Gets or sets the ConsultantStatus currently selected in the workspace
        /// </summary>
        public string CurrentConsultantStatus
        {
            get
            {
                return this.currentConsultantStatus;
            }

            set
            {
                this.currentConsultantStatus = value;
                this.OnPropertyChanged("CurrentConsultantStatus");
            }
        }

        /// <summary>
        /// the ConsultantAccreditationDetail currently selected in the workspace
        /// </summary>
        private string currentConsultantAccreditationDetail;
        /// <summary>
        /// Gets or sets the ConsultantAccreditationDetail currently selected in the workspace
        /// </summary>
        public string CurrentConsultantAccreditationDetail
        {
            get
            {
                return this.currentConsultantAccreditationDetail;
            }

            set
            {
                this.currentConsultantAccreditationDetail = value;
                this.OnPropertyChanged("CurrentConsultantAccreditationDetail");
            }
        }

        /// <summary>
        /// The depatment currently selected in the workspace
        /// </summary>
        private Department currentConsultantDepartment;

        /// <summary>
        /// Gets or sets the depatment currently selected in the workspace
        /// </summary>
        public Department CurrentConsultantDepartment
        {
            get
            {
                return this.currentConsultantDepartment;
            }

            set
            {
                this.currentConsultantDepartment = value;
                this.OnPropertyChanged("CurrentConsultantDepartment");
            }
        }

        /// <summary>
        /// The Branch currently selected in the workspace
        /// </summary>
        private Branch currentConsultantBranch;

        /// <summary>
        /// Gets or sets the Branch currently selected in the workspace
        /// </summary>
        public Branch CurrentConsultantBranch
        {
            get
            {
                return this.currentConsultantBranch;
            }

            set
            {
                this.currentConsultantBranch = value;
                this.OnPropertyChanged("CurrentConsultantBranch");
            }
        }

        
    }
}
